package cn._2dland.uploader.utils;

public class UploadUtils {
	/**
	 * 格式化百分比，保留两位
	 * @param value
	 * @return
	 */
	public static String formatPercent(double value) {
		double percent = (double)Math.round(value * 10000) / 100;
		return percent + "%";
	}

	/**
	 * 格式化速度，保留一位
	 * @param value
	 * @return
	 */
	public static String formatSpeed(long value) {
		String speed = "0B/s";
		if(value < 0x400) {
			speed = value + "B/s";
		}
		else if(value < 0x100000) {
			double kbps = (double)Math.round(value * 10 / 0x400) / 10;
			speed = kbps + "KB/s";
		}
		else {
			double mbps = (double)Math.round(value * 10 / 0x100000) / 10;
			speed = mbps + "MB/s";
		}
		return speed;
	}

	public static String formatTime(long value) {
		String time = "0s";
		if(value < 60) {
			time = value + "s";
		}
		else if(value < 3600) {
			time = (value / 60) + "m:" + (value % 60) + "s";
		}
		else if(value <= 86400) {
			time = (value / 1440) + "h:" + ((value / 60) % 24) + "m:" + (value % 60) + "s";
		}
		else {
			time = "大于一天，放弃吧";
		}
		return time;
	}
}
